package com.exchange.controller;

import com.alibaba.fastjson2.JSON;
import com.exchange.entity.model.AdminTokenModel;
import com.exchange.entity.model.AdminUserInfo;
import com.exchange.entity.pojo.User;
import com.exchange.service.UserService;
import com.exchange.until.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


/**
 * @Author : RulerWood
 * @Data : 2024/4/23 - 16:29
 */
@Slf4j
@RestController
@Tag(name = "登录接口")
@RequestMapping("/common")
public class AdminLoginController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录接口")
    public Result<AdminTokenModel> login(@RequestBody User user) {
        return userService.login(user);
    }

    
    @GetMapping("/userinfo")
    @Operation(summary = "管理员获取用户信息")
    public Result<AdminUserInfo> userinfo(@RequestHeader(name = "token") String token) {
        log.info("token=====>{}" + token);
        String s = redisTemplate.opsForValue().get("user:login" + token);
        User user = JSON.parseObject(s, User.class);
        AdminUserInfo adminUserInfo = new AdminUserInfo();
        assert user != null;
        adminUserInfo.setName(user.getName());
        adminUserInfo.setAvatar(user.getAvatar());
        return Result.success(adminUserInfo);
    }


}
